//
//  ImageShowerControllerViewController.m
//  SouthChina
//
//  Created by creator_lq on 15/1/16.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "ImageShowerController.h"

@interface ImageShowerController (){
    
    UIImageView * imgeview;
}

@end

@implementation ImageShowerController

@synthesize filePath;

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self addTitleBarWithBackBtn:@"图片详情" needReturn:YES];
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, TITLE_BAR_HEGIHT+(YNEW), SCREEN_WIDTH, self.view.frame.size.height-TITLE_BAR_HEGIHT-20)];
    
    scroll.delegate = self;
    scroll.minimumZoomScale = 0.2;
    scroll.maximumZoomScale = 5.0;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setBackgroundImage:[[UIImage imageNamed:@"btntop_right_over"]stretchableImageWithLeftCapWidth:10 topCapHeight:10] forState:UIControlStateNormal];
    [rightBtn setBackgroundImage:[[UIImage imageNamed:@"btntop_right_over_press"]stretchableImageWithLeftCapWidth:10 topCapHeight:10] forState:UIControlStateHighlighted];
    [rightBtn setTitle:@"保存至相册" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(SCREEN_WIDTH-85,5+(YNEW), 75.0f, 30.0f);
    [rightBtn addTarget:self action:@selector(storePhote) forControlEvents:UIControlEventTouchDown];
    [self.titlebar addSubview:rightBtn];
    rightBtn.hidden = YES;
    
    imgeview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height-TITLE_BAR_HEGIHT-20)];
    imgeview.contentMode = UIViewContentModeScaleAspectFit;

    scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:scroll];
    [scroll addSubview:imgeview];
    
    [imgeview setImageWithURL:[NSURL URLWithString:filePath] placeholderImage:[UIImage imageNamed:@"empty_photo"] success:^(UIImage *image) {
        rightBtn.hidden = NO;

    } failure:^(NSError *error) {
        rightBtn.hidden = YES;

    }];
    

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return  imgeview;
}

-(void)storePhote{
    
    UIImageWriteToSavedPhotosAlbum(imgeview.image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error == nil)
    {
        [self showAlerHUD:@"保存成功！"];
    }
    else
    {
        [self showAlerHUD:@"保存失败！"];
    }
}


@end
